﻿/*
COPYRIGHT (C) 2009 EPISERVER AB

THIS FILE IS PART OF REMOTE PAGE PROVIDER FOR EPISERVER CMS.
REMOTE PAGE PROVIDER FOR EPISERVER CMS IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

REMOTE PAGE PROVIDER FOR EPISERVER CMS IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS.
YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH REMOTE PAGE PROVIDER FOR EPISERVER CMS]. IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/
using System;
using System.Collections.Generic;
using System.Text;
using EPiServer.DataAbstraction;
using EPiServer.Security;
using EPiServer.Core;

namespace EPiServer.Labs.RemoteProvider
{
    internal static class RemotePageHelper
    {
        public delegate void Parse<T>(string value, ref T outValue);

        /// <summary>
        /// Gets the property.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data">The data.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="Tvalue">The tvalue.</param>
        /// <param name="parse">The parse.</param>
        /// <returns></returns>
        public static bool GetProperty<T>(Dictionary<string, string> data, string propertyName, ref T Tvalue, Parse<T> parse) 
        {
            string value;
            if (data.TryGetValue(propertyName, out value))
            {
                if (parse != null)
                {
                    parse(value.ToString(), ref  Tvalue);
                    return true;
                }    
            }
            return false;
        }


        /// <summary>
        /// Gets the language selector.
        /// </summary>
        /// <param name="pageLink">The page link.</param>
        /// <param name="selector">The selector.</param>
        /// <returns></returns>
        public static LanguageSelector GetLanguageSelector(PageReference pageLink, ILanguageSelector selector)
        {
            LanguageSelector newSelector = selector as LanguageSelector;

            if (newSelector != null)
            {
                return newSelector;
            }

            LanguageSelectorContext args = new LanguageSelectorContext(pageLink);
            selector.LoadLanguage(args);

            return new LanguageSelector(args.SelectedLanguage);
        }


        /// <summary>
        /// Packs the type of the page data.
        /// </summary>
        /// <param name="pageData">The page data.</param>
        /// <returns></returns>
        public static Dictionary<string, string> PackPageDataType(PageData pageData)
        {
            Dictionary<string, string> dataType = new Dictionary<string, string>(pageData.Property.Count);

            foreach (PropertyData prop in pageData.Property)
            {
                if (prop.Type.ToString() != string.Empty)
                {
                    dataType.Add(prop.Name, prop.Type.ToString());
                }
                else
                {
                    dataType.Add(prop.Name, string.Empty);
                }
            }
            return dataType;
        }

    }
}
